package com.vasthu.core.service;

import com.vasthu.api.dao.CategoryDAO;
import com.vasthu.api.domain.Category;
import com.vasthu.api.service.CategoryManagerService;
import com.vasthu.core.dao.CategoryDAOImpl;
import com.vasthu.gui.VstTableItemSelectable;
import com.vasthu.utility.VstDBManager;
import java.util.*;
import java.sql.*;

public class CategoryManagerServiceImpl implements CategoryManagerService {

	private CategoryDAO catDao;

	public CategoryManagerServiceImpl() {
		catDao = new CategoryDAOImpl();
	}
	
	public boolean insertCategory(Category cat) {
		if (cat == null)
			return false;
			
		Connection conn = VstDBManager.get().getConnection();
		boolean ok = false;
		try {
			ok = catDao.insertCategory(conn, cat);
		} catch (Exception ex) {
			System.out.println("ERROR : CategoryManagerServiceImpl->insertCategory()");
		}
		
		VstDBManager.get().close();
		return ok;
	}
	
	public Category getCategoryById(long catId) {
		Connection conn = VstDBManager.get().getConnection();
		Category cat = null;
		try {
			cat = catDao.getCategoryById(conn, catId);
		} catch (Exception ex) {
			System.out.println("ERROR : CategoryManagerServiceImpl->deleteCategoryById()");
		}
		
		VstDBManager.get().close();
		return cat;
	}
	
	public Category getCategoryByName(String catName) {
		Connection conn = VstDBManager.get().getConnection();
		Category cat = null;
		try {
			cat = catDao.getCategoryByName(conn, catName);
		} catch (Exception ex) {
			System.out.println("ERROR : CategoryManagerServiceImpl->deleteCategoryByName()");
		}
		
		VstDBManager.get().close();
		return cat;
	}
	
	public boolean deleteCategoryById(long catId) {
		Connection conn = VstDBManager.get().getConnection();
		boolean ok = false;
		try {
			ok = catDao.deleteCategoryById(conn, catId);
		} catch (Exception ex) {
			System.out.println("ERROR : CategoryManagerServiceImpl->deleteCategoryById()");
		}
		
		VstDBManager.get().close();
		return ok;
	}
	
	public List<String> getAllCategoryNames() {
		List<Category> catList = new ArrayList<Category>();
		List<String> catNameList = new ArrayList<String>();
		
		Connection conn = VstDBManager.get().getConnection();
		
		try {
			catList = catDao.getAllCategorys(conn);
			for (Category cat : catList) {
				catNameList.add(cat.getCategoryName());
			}
		} catch (Exception ex) {
			System.out.println("ERROR : CategoryManagerServiceImpl->getAllCategoryNames()");
		}
		
		VstDBManager.get().close();
		
		return catNameList;
	}

	public List<VstTableItemSelectable> getAllCategorysForTable() {
		List<Category> catList = new ArrayList<Category>();
		List<VstTableItemSelectable> dataList = new ArrayList<VstTableItemSelectable>();
		
		Connection conn = VstDBManager.get().getConnection();
		
		try {
			catList = catDao.getAllCategorys(conn);
			for (Category cat : catList) {
				dataList.add(cat);
			}
		} catch (Exception ex) {
			System.out.println("ERROR : CategoryManagerServiceImpl->getAllCategorysForTable()");
		}
		
		VstDBManager.get().close();
		
		return dataList;
	}

}